package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.provider.api.model.vo.RoleVo;
import com.weixing.mall.provider.api.service.SysUserRpcService;
import com.weixing.mall.provider.mapper.AgentRefundMapper;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.domain.AgentLog;
import com.weixing.mall.provider.model.domain.AgentRefund;
import com.weixing.mall.provider.model.dto.AgentRefundDto;
import com.weixing.mall.provider.model.enums.AgentLogTypeEnum;
import com.weixing.mall.provider.model.query.AgentRefundQuery;
import com.weixing.mall.provider.model.vo.AgentRefundVo;
import com.weixing.mall.provider.service.IAgentLogService;
import com.weixing.mall.provider.service.IAgentRefundService;
import com.weixing.mall.provider.service.IAgentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 代理商模块-代理商退费记录表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentRefundServiceImpl extends ServiceImpl<AgentRefundMapper, AgentRefund> implements IAgentRefundService {

    @Autowired
    private IAgentService agentService;
    @Autowired
    private SysUserRpcService sysUserRpcService;
    @Autowired
    private IAgentLogService agentLogService;

    @Override
    public IPage<AgentRefundVo> selectOfPage(Page page, AgentRefundQuery agentRefundQuery,SysAuth auth) {
        if(null!=auth){
            Long uid=auth.getUid();
            List<RoleVo> list= sysUserRpcService.getRoleById(uid).getData();
            for(RoleVo vo:list){
                if(vo.getRoleCode().equals(GlobalConstant.RoleCode.ADMIN)){
                    return baseMapper.selectListOfPage(page, agentRefundQuery);
                }
            }
            for(RoleVo vo:list){
                if(vo.getRoleCode().equals(GlobalConstant.RoleCode.KFADMIN)){
                    return baseMapper.selectListOfPage(page, agentRefundQuery);
                }
            }
            agentRefundQuery.setCreatId(uid);
            return baseMapper.selectListOfPage(page, agentRefundQuery);
        }else {
            return baseMapper.selectListOfPage(page, agentRefundQuery);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AgentRefundDto agentRefundDto, SysAuth auth) {
        //新增退费记录
        AgentRefund agentRefund = new AgentRefund();
        BeanUtils.copyProperties(agentRefundDto, agentRefund);
        agentRefund.setAgentLevel(agentRefundDto.getAgentLevel().getValue());
        agentRefund.setCreatedBy(auth.getUid());
        agentRefund.setCreator(auth.getRealName());
        save(agentRefund);
        //将代理商状态修改为禁用
        Agent agent = new Agent();
        agent.setId(agentRefundDto.getAgentId());
        agent.setIsEnable(IsEnableEnum.DISABLE.getValue());
        agentService.updateById(agent);

        AgentLog log = new AgentLog();
        log.setAgentId(agent.getId());
        log.setAgentSn(agent.getAgentSn());
        log.setAgentName(agent.getAgentName());
        log.setAgentLevel(agent.getAgentLevel());
        log.setMobile(agent.getMobile());
        log.setWechatNum(agent.getWechatNum());
        log.setOperatorId(auth.getUid());
        log.setOperatorName(auth.getRealName());
        log.setAmount(agentRefund.getAmount());
        log.setLogType(AgentLogTypeEnum.TK.getValue());
        log.setCustomerId(auth.getUid());
        log.setCustomerName(auth.getRealName());
        agentLogService.save(log);
    }
}
